<?php
function scan_modules()
{
    $dir = PUNG_ROOT . '/modules/';
    $dh = opendir($dir);
    $files = array();
   
    while(false !== ($filename = readdir($dh)))
    {
        if($filename != "." && $filename != "..") {
            $files[] = $filename;
        }
    }

    sort($files);
   
    return $files;
}

function convert_bytes($number)
{
	$len = strlen($number);
	
	if($len < 4)
	{
		return sprintf("%d b", $number);
	}
	if($len >= 4 && $len <=6)
	{
		return sprintf("%0.2f KB", $number/1024);
	}
	if($len >= 7 && $len <=9)
	{
		return sprintf("%0.2f MB", $number/1024/1024);
	}
		return sprintf("%0.2f GB", $number/1024/1024/1024);   
}

function redirect_with_message($type, $message, $place = '0')
{
	if($place == '0')
	{
		$file = basename($_SERVER['SCRIPT_FILENAME']);
	}
	else
	{
		$file = $place;
	}
	
	header('Location: ' . $file . '?' . $type . '=' . $message);
	die();
}

function get_contents($file)
{
	$str = fopen($file, 'rb');
	$contents = fread($str, filesize($file));
	fclose($str);
	
	return $contents;
}

function filenameCleanup($str)
{
	$str = preg_replace('/\ /', '_', $str);
	$str = preg_replace('/[^a-zA-Z_\.]/', '', $str);
	$str = strtolower($str);
	
	return $str;
}

function debug($str)
{
	print '<pre style="background-color: grey">';
	print_r($str);
	print '</pre>';
}

function mysql_is_table($tbl)
{
	$tables = array();
	$q = @mysql_query("SHOW TABLES");
	while ($r = @mysql_fetch_array($q)) { $tables[] = $r[0]; }
	@mysql_free_result($q);
	if (in_array($tbl, $tables)) { return TRUE; }
	else { return FALSE; }
}

function file_extension($strFileName)
{
	return strtolower(substr(strrchr($strFileName, '.'), 1));       
}

function table_name($str)
{
	$str = DB_PREFIX . $str;
	
	return $str;
}
?>